﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Collections.Specialized;

namespace MyHealthByDesign.Domain.Panels
{
    public class TextBoxWithChecklist : IQuestionPanel
    {
        #region IQuestionPanel Members

        public System.Web.UI.WebControls.Panel GenerateQuestionDisplay(Question q)
        {
            Panel pnlIn = new Panel();

            List<string> FieldNames = new List<string>();
            List<string> FieldSuffixes = new List<string>();
            List<string> FieldCSSs = new List<string>();
            
            // load Generic Lists
            FieldNames =  new List<string>(q.Name.Split('|'));
            
            if (!string.IsNullOrEmpty(q.FieldCSS))
                FieldCSSs =  new List<string>(q.FieldCSS.Split('|'));

            if (!string.IsNullOrEmpty(q.FieldSuffix))
                FieldSuffixes =  new List<string>(q.FieldSuffix.Split('|'));
           
            if (FieldNames.Count < 2)
            {
                SimpleTextBox stb = new SimpleTextBox();
                return stb.GenerateQuestionDisplay(q);
            }
            else if (FieldNames.Count > 2)
            {
                MultipleTextBoxes mtb = new MultipleTextBoxes();
                return mtb.GenerateQuestionDisplay(q);
            }

            Label lblC = new Label();
            lblC.Text = q.ContentLabel;
            lblC.CssClass = "panelLeft";

            pnlIn.Controls.Add(lblC);
            
            Type typField = typeof(TextBox).Assembly.GetType("System.Web.UI.WebControls.TextBox");
            TextBox tb = (TextBox)Activator.CreateInstance(typField);
            tb.ID = FieldNames[0];
            if (!string.IsNullOrEmpty(q.FieldCSS))
                tb.CssClass = FieldCSSs[0];
            
            pnlIn.Controls.Add(tb);

            if (!string.IsNullOrEmpty(q.FieldSuffix))
            {
                Label lblSuffix = new Label();
                lblSuffix.Text = "&nbsp;&nbsp;" + FieldSuffixes[0] + "&nbsp;&nbsp;";
                pnlIn.Controls.Add(lblSuffix);
            }

            Type typField2 = typeof(CheckBox).Assembly.GetType("System.Web.UI.WebControls.CheckBox");
            CheckBox cb = (CheckBox)Activator.CreateInstance(typField2);
            cb.ID = FieldNames[1];
            if (!string.IsNullOrEmpty(q.FieldCSS))
                cb.CssClass = FieldCSSs[1];
            
            pnlIn.Controls.Add(cb);

            if (!string.IsNullOrEmpty(q.FieldSuffix))
            {
                Label lblSuffix2 = new Label();
                lblSuffix2.Text = "&nbsp;&nbsp;" + FieldSuffixes[1] + "&nbsp;&nbsp;";
                pnlIn.Controls.Add(lblSuffix2);
            }

            

            if (!string.IsNullOrEmpty(q.FieldHelpText) && !string.IsNullOrEmpty(q.FieldHelpURL))
            {
                HtmlGenericControl aHelp = new HtmlGenericControl("a");
                aHelp.Attributes.Add("href", q.FieldHelpURL);
                aHelp.Attributes.Add("target", "_blank");
                aHelp.InnerText = q.FieldHelpText;
                pnlIn.Controls.Add(aHelp);
            }

            return pnlIn;
        }

        
        public Dictionary<Question, System.Collections.Specialized.NameValueCollection> RetrieveQuestionValue(Question q, ContentPlaceHolder cph)
        {
            Dictionary<Question, NameValueCollection> dFieldValues = new Dictionary<Question, NameValueCollection>();
            NameValueCollection nvc = new NameValueCollection();

            List<string> FieldNames =  new List<string>(q.Name.Split('|'));

            TextBox tb = (TextBox)cph.FindControl(FieldNames[0]);
            nvc.Add(FieldNames[0], tb.Text);

            CheckBox cb = (CheckBox)cph.FindControl(FieldNames[1]);
            nvc.Add(FieldNames[1], cb.Checked.ToString());

            dFieldValues.Add(q, nvc);

            return dFieldValues;
        }

        #endregion
    }
}